package com.beadhouse.bed.support.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.filter.CharacterEncodingFilter;

import com.beadhouse.bed.ao.BedAo;
import com.beadhouse.bed.dao.BedDao;
import com.beadhouse.bed.service.BedService;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.BedEntity;

import lombok.NonNull;
@Service
public class BedServiceImpl implements BedService {
	final BedDao bdao;
	public BedServiceImpl(BedDao bdao) {
		this.bdao=bdao;
	}
	@Override
	public BedEntity addBed(@NonNull BedAo ao)throws Exception {
		int CreateId=Integer.parseInt(ao.getCreateId());
		//1验证参数
		String name=ao.getName();
		//2执行业务逻辑
		BedEntity e=new BedEntity();
	    e.setCreateTime(new Date());
		e.setCreateBy(CreateId);//创建人
		e.setUpdateTime(new Date());
		e.setUpdateBy(CreateId);
		e.setIsDeleted(false);
		e.setRoomNumber("110110");
		e.setBedStatus(false);
		e.setRemark(null);
		e.setName(name);
		e.setSort(null);
		e.setEndTime(new Date());
		e.setUseCustomerId(null);
		bdao.insert(e);
		return e;
	}
    //通过顾客id修改床的状态 例退住
	@Override
	public void modifyBed(BedAo ao) throws Exception {	
		if(ao.getCustomerId()==null) {
			throw new BeadHouseException("办理退住顾客ID不能为空");
		}				
		int id=Integer.parseInt(ao.getCustomerId());
		//2执行业务逻辑
		Map<String, Object> map=new HashMap<>();	
		map.put("remark", 1);
		//map.put("updateBy", ao.getCreateId());
		map.put("updateTime", new Date());
		bdao.update(id, map);
	
	}

	@Override
	public BedEntity queryBed(BedAo ao) throws Exception {
		int id=Integer.parseInt(ao.getId());
		//2执行业务逻辑
		
		BedEntity bed=bdao.selectById(id);
		return bed;
	}

	@Override
	public void deleteBed(BedAo ao) throws Exception {
		
		
		int id=Integer.parseInt(ao.getId());
		//2执行业务逻辑
		Map<String, Object> map=new HashMap<>();
		map.put("isDeleted",false);
		map.put("updateBy", ao.getCreateId());
		map.put("updateTime", new Date());
		bdao.update(id, map);
		
		
	}
	@Override
	public List queryAllBed(BedAo ao) throws Exception {
		
		//2执行业务逻辑
		List<BedEntity> list =bdao.selectList();
		return list;
	}

	@Override
	public void addBedCustomer(BedAo ao) throws Exception {
		if(ao.getId()==null) {
			throw new BeadHouseException("床位ID不能为空");
		}
		if(ao.getCustomerId()==null) {
			throw new BeadHouseException("顾客ID不能为空");
		}
		int id=Integer.parseInt(ao.getId());
		int CustomerId=Integer.parseInt(ao.getCustomerId());
		
		Map<String, Object> map=new HashMap<>();
		System.out.println("AAAAA"+CustomerId);
		map.put("useCustomerId",CustomerId);
		map.put("bedStatus", true);
		map.put("remark", null);
		map.put("updateTime",new Date());
		map.put("updateBy", ao.getCreateId());
		bdao.updateSelective(id, map);
	}

	@Override
	public void deleteBedCustomer(BedAo ao) throws Exception{
		if(ao.getId()==null) {
			throw new BeadHouseException("床位ID不能为空");
		}
		int id=Integer.parseInt(ao.getId());
		Map<String, Object> map=new HashMap<>();
		map.put("UseCustomerId",null);
		map.put("bedStatus", false);
		map.put("remark", null);
		map.put("updateTime",new Date());
		map.put("updateBy", ao.getCreateId());
		bdao.updateSelective(id, map);	
		
	}

	@Override
	public List<BedEntity> queryAllfreeBed() throws Exception {
		List<BedEntity> list =bdao.selectFreeList();
		return list;
	}

	
}
